package com.uam.core.business;

import com.uam.core.entity.PointInfoData;
import com.uam.core.lk.utils.GeoTools;
import com.uam.core.lk.utils.RegionUtil;
import com.uam.core.lk.utils.fencebuffer.BufferBase;
import com.uam.core.tool.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 根据时间找到对应的轨迹点位置
 */
@Slf4j
public class TimeToPointIndex {
     /**
      * 缓冲区域的大小，是飞行器安全距离的倍数
      */
    private static Integer SAFE_DISTANCE_MULTIPLE=6;

    public static PointInfoData newPointData(List<PointInfoData> listPoint, long nowTime, Integer taskId){
        int row = listPoint.size();

        PointInfoData start = listPoint.get(0);
        //最后一个
        PointInfoData end = listPoint.get(row - 1);


        if(nowTime<start.getTime()){
            return null;
        }

        if (nowTime >= end.getTime()) {
            return null;
        }

        int sIndex=0;
        int eIndex=0;
        Long sTime=0L;
        Long eTime=0L;
        PointInfoData sPoint=null;
        PointInfoData ePoint=null;
        int rowIndex=listPoint.size();
        for(int i=0;i<rowIndex-1;i++){
            int sIndexTemp=i;
            int eIndexTemp=i+1;
            PointInfoData sPointTemp= listPoint.get(sIndexTemp);
            PointInfoData ePointTemp= listPoint.get(eIndexTemp);
            sTime=sPointTemp.getTime();
            eTime=ePointTemp.getTime();

            if(nowTime>sTime && nowTime<=eTime){
                sIndex=sIndexTemp;
                eIndex=eIndexTemp;
                sPoint=sPointTemp;
                ePoint=ePointTemp;
                break;
            }
        }

        if(sIndex !=0 && eIndex !=0) {
            double sL = sPoint.getLon();
            double sY = sPoint.getLat();
            double eL = ePoint.getLon();;
            double eY = ePoint.getLat();
            long sTime1 = listPoint.get(sIndex).getTime();
            long sTime2 = (nowTime - sTime1)/1000;
            double speed = sPoint.getSpeed();
            double speedM = speed * 1000 / 3600;
            //飞行距离
            double distanceTemp = speedM * (sTime2);
            //总距离
            double tempSum = RegionUtil.computeDistance(sL, sY, eL, eY);
            double reate = distanceTemp / tempSum;
            Coordinate point = GeoTools.getPointOnLine(new Coordinate(sL, sY), new Coordinate(eL, eY), reate);
           if(!GeoTools.onLineSegment(point,new Coordinate(sL, sY), new Coordinate(eL, eY))){
               point=new Coordinate(eL,eY);
           }
            return new PointInfoData(nowTime,
                    point.x,point.y,
                    sPoint.getHeight(),
                    eIndex,sPoint.getTaskId(),
                    sPoint.getSafeDistance()* SAFE_DISTANCE_MULTIPLE,
                    speed,end.getTime(),sPoint.getBuffer(),sPoint.getFlightInterval(),sPoint.getTaskName(),sPoint.getId());

        }
        return null;
    }

    public static LineString getLineString(List<PointInfoData> pointInfoData){
        Coordinate[] coordinates = new Coordinate[pointInfoData.size()];
        for (int i = 0; i < pointInfoData.size(); i++) {
            PointInfoData pointInfoData1 = pointInfoData.get(i);
            Coordinate coordinate = new Coordinate(pointInfoData1.getLon(), pointInfoData1.getLat());
            coordinates[i] = coordinate;
        }
        return BufferBase.geometryFactory.createLineString(coordinates);
    }

    public void solveConflict(Map<Integer, Map<String, PointInfoData>> integerMapMap, Geometry bufferedGeom, Integer takId){

        integerMapMap.forEach((k,v)->{
            if(!k.equals(takId)){
                List<PointInfoData> pointInfoData= new ArrayList<>(v.values());

                LineString lineString1 = TimeToPointIndex.getLineString(pointInfoData);
                Geometry geometry =   bufferedGeom.intersection(lineString1);
                Geometry geometry1= bufferedGeom.intersection(geometry);
                if(geometry1 !=null&& !geometry1.isEmpty()){
                    log.info("冲突点：{}",geometry1.toText());
                }
            }
        });
    }

    /**
     * 基站故障后，检测信息
     * @param listPoint
     * @param nowTime
     * @param taskId
     * @return
     */
    public static PointInfoData checkPointDataConn(List<PointInfoData> listPoint, long nowTime, Integer taskId){
        int row = listPoint.size();

        PointInfoData start = listPoint.get(0);
        //最后一个
        PointInfoData end = listPoint.get(row - 1);


        if(nowTime<start.getTime()){
            return null;
        }

        if (nowTime >= end.getTime()) {
            return null;
        }

        int sIndex=0;
        int eIndex=0;
        Long sTime=0L;
        Long eTime=0L;
        PointInfoData sPoint=null;
        PointInfoData ePoint=null;
        int rowIndex=listPoint.size();
        for(int i=0;i<rowIndex-1;i++){
            int sIndexTemp=i;
            int eIndexTemp=i+1;
            PointInfoData sPointTemp= listPoint.get(sIndexTemp);
            PointInfoData ePointTemp= listPoint.get(eIndexTemp);
            sTime=sPointTemp.getTime();
            eTime=ePointTemp.getTime();

            if(nowTime>sTime && nowTime<=eTime){
                sIndex=sIndexTemp;
                eIndex=eIndexTemp;
                sPoint=sPointTemp;
                ePoint=ePointTemp;
                break;
            }
        }

        if(sIndex !=0 && eIndex !=0) {
            double sL = sPoint.getLon();
            double sY = sPoint.getLat();
            double eL = ePoint.getLon();;
            double eY = ePoint.getLat();
            long sTime1 = listPoint.get(sIndex).getTime();
            long sTime2 = (nowTime - sTime1)/1000;
            double speed = sPoint.getSpeed();
            double speedM = speed * 1000 / 3600;
            //飞行距离
            double distanceTemp = speedM * (sTime2);
            //总距离
            double tempSum = RegionUtil.computeDistance(sL, sY, eL, eY);
            double reate = distanceTemp / tempSum;
            Coordinate point = GeoTools.getPointOnLine(new Coordinate(sL, sY), new Coordinate(eL, eY), reate);
            if(!GeoTools.onLineSegment(point,new Coordinate(sL, sY), new Coordinate(eL, eY))){
                point=new Coordinate(eL,eY);
            }
            return new PointInfoData(nowTime,
                    point.x,point.y,
                    sPoint.getHeight(),
                    sIndex,sPoint.getTaskId(),
                    sPoint.getSafeDistance()* SAFE_DISTANCE_MULTIPLE,
                    speed,end.getTime(),sPoint.getBuffer(),sPoint.getFlightInterval(),sPoint.getTaskName(),sPoint.getId());

        }
        return null;
    }

}
